﻿using LocalNas.Application.Service.Dto;
using LocalNas.Core.Const;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LocalNas.Application.Service
{
    [ApiDescriptionSettings(ConstSwaggerGroup.NAS平台, Name = "用户服务", Order = 100)]
    public class SysUserService : IDynamicApiController, ITransient
    {
        private readonly SqlSugarRepository<SysUser> _sysUserRep;
        private readonly SqlSugarRepository<SysUserFolder> _sysUserFolderRep;
        public SysUserService(SqlSugarRepository<SysUser> sysUserRep,
            SqlSugarRepository<SysUserFolder> sysUserFolderRep)
        {
            _sysUserRep = sysUserRep;
            _sysUserFolderRep = sysUserFolderRep;
        }

        /// <summary>
        /// 全部用户
        /// </summary>
        /// <returns></returns>
        [HttpPost("/SysUser/AllUser")]
        public async Task<dynamic> AllUser()
        {
            var list = await _sysUserRep.AsQueryable()
                .Select(x => (dynamic)new
                {
                    x.Id,
                    x.Name,
                    x.Account,
                    UserType = "",
                    UserTypeEnum = x.UserType
                })
                .Mapper(x =>
                {
                    x.UserType = ((UserTypeEnum)x.UserTypeEnum).ToString();
                })
                .ToListAsync();

            return list;
        }

        /// <summary>
        /// 获取当前用户允许访问的目录
        /// </summary>
        /// <returns></returns>
        [HttpPost("/SysUser/GetFolderByUser")]
        public async Task<List<SysUserFolder>> GetFolderByUser(long userId)
        {
            var list = await _sysUserFolderRep.AsQueryable().Where(x => x.UserId == userId)
                   .ToListAsync();
            return list;
        }

        /// <summary>
        /// 设置用户允许访问的目录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("/SysUser/SetUserFolder")]
        public async Task SetUserFolder(SetUserFolderRequest request)
        {
            long.TryParse(App.User.FindFirst(ClaimConst.UserId)?.Value, out long userId);
            await _sysUserFolderRep.DeleteAsync(x => x.UserId == userId);
            var list = request.Folders.Select(x => new SysUserFolder
            {
                UserId = request.UserId,
                Path = x.Path,
                Half = x.Half
            }).ToList();
            await _sysUserFolderRep.AsInsertable(list).ExecuteCommandAsync();
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpGet("/SysUser/ModifyPassword")]
        public async Task ModifyPassword(string password)
        {
            long.TryParse(App.User.FindFirst(ClaimConst.UserId)?.Value, out long userId);
            var encryptPassword = MD5Encryption.Encrypt(password); // 加密密码
            await _sysUserRep.AsUpdateable()
           .SetColumns(u => u.Password == encryptPassword)
           .Where(x => x.Id == userId)
           .ExecuteCommandAsync();
        }
    }
}
